package com.dk.service.es;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.dk.config.DongDongSearchConfig;
import com.dk.pojo.item.TbItem;
import com.dk.service.item.ItemService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Service
@Component
public class EsServiceImpl implements EsService {

    @Resource
    private RestHighLevelClient restHighLevelClient;

    @Reference
    private ItemService itemService;

    @Override
    public void saveEs(Long id) {
        List<TbItem> itemList = itemService.getItemByGoodsId(id);
        TbItem item = itemList.get(0);
        IndexRequest request = new IndexRequest("dongdongshop", "item", item.getGoodsId().toString());
        //方式1
//        request.source("userName","zhangsan","age","18");
        try {
            //方式二
            String userJson = new ObjectMapper().writeValueAsString(item);
            request.source(userJson, XContentType.JSON);
            //执行操作
            IndexResponse response = restHighLevelClient.index(request, DongDongSearchConfig.COMMON_OPTIONS);
            //提取有用的响应信息
            System.out.println(">>>index: "+response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getItem(Integer pageNumber, Integer pageSize, String title) {
        //  创建索引请求
        SearchRequest searchRequest = new SearchRequest();
        // 指定索引
        searchRequest.indices("dongdongshop");
        // 指定条件
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.from(pageNumber-1);
        searchSourceBuilder.size(pageSize);
        searchSourceBuilder.query(QueryBuilders.matchQuery("title",title));
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        HighlightBuilder.Field highlightTitle =new HighlightBuilder.Field("title");
        highlightTitle.fragmentSize(5);
        highlightTitle.numOfFragments(1);
        highlightTitle.preTags("<span color=red>");
        highlightTitle.postTags("</span>");
        highlightBuilder.field(highlightTitle);
        searchSourceBuilder.highlighter(highlightBuilder);

        System.out.println(">>>检索条件 : "+searchSourceBuilder);
        // 发送
        searchRequest.source(searchSourceBuilder);
        //执行检索
        SearchResponse search = null;
        try {
            search = restHighLevelClient.search(searchRequest, DongDongSearchConfig.COMMON_OPTIONS);
            //分析结果
            System.out.println(">>>返回结果"+search.toString());
            SearchHits hits = search.getHits();
            SearchHit[] innerHis = hits.getHits();
            for (SearchHit hit : innerHis) {
                String sourceAsStringOfUser = hit.getSourceAsString();
                ObjectMapper objectMapper = new ObjectMapper();
                TbItem item = objectMapper.readValue(sourceAsStringOfUser, TbItem.class);
                System.out.println("转换后的 User为:"+item);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return search.toString();
    }
}
